//
//  CalendarView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/4.
//  Copyright © 2019 李文强. All rights reserved.
//

#import "CalendarView.h"

@interface CalendarView(){
    NSInteger yearString;
    NSInteger monthString;
    NSInteger dayString;
}
@property (nonatomic, strong) GetDateSuccessBlock block;
@end

@implementation CalendarView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.frame = CGRectMake(0, 0, 350, 400);
    
    
}
+(void)initWithDateWithFrame:(CGRect)frame andeSourceView:(UIView *)view andDelegate:(UIViewController *)controller andSuccessBlock:(GetDateSuccessBlock)success{
    CalendarView *canderView = [[CalendarView alloc]initWithSetupCalendarViewBlock:success];
    canderView.preferredContentSize = CGSizeMake(400, 400);
    canderView.view.backgroundColor = [UIColor whiteColor];
    canderView.popoverPresentationController.sourceView = view;
    canderView.modalPresentationStyle = UIModalPresentationPopover;
    canderView.popoverPresentationController.sourceView = view;
    canderView.popoverPresentationController.sourceRect = frame;
    canderView.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionDown;
    canderView.type = YES;
    [controller presentViewController:canderView animated:YES completion:nil];
}
// 初始化calendarView
- (instancetype)initWithSetupCalendarViewBlock:(GetDateSuccessBlock)success{
    if ([super init]) {
        _block = success;
        GFCalendarView *calendarView = [[GFCalendarView alloc] initWithFrameOrigin:CGPointMake(25, 15) width:self.view.frame.size.width];
        calendarView.didSelectDayHandler = ^(NSInteger year, NSInteger month, NSInteger day) {
            NSLog(@"=====%zd=====%zd=====%zd", year, month, day);
            self->yearString = year;
            self->monthString = month;
            self->dayString = day;
        };
        [self.view addSubview:calendarView];
        [self.view addSubview:self.confirmBtn];
        [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@20);
            make.right.equalTo(@0).offset(-20);
            make.height.equalTo(@40);
            make.bottom.equalTo(@0).offset(-10);
        }];
        
    }
    return self;
}
- (UIButton *)confirmBtn{
    if (!_confirmBtn) {
        UIButton *sender = [UIButton buttonWithType:UIButtonTypeCustom];
        [sender setTitle:@"确定" forState:UIControlStateNormal];
        [sender setBackgroundImage:[UIImage imageWithColor:SELECTEDCOLOR forSize:CGSizeMake(120, 40)] forState:UIControlStateNormal];
        sender.layer.cornerRadius = 4;
        sender.titleLabel.font = [UIFont systemFontOfSize:14];
        sender.layer.masksToBounds = YES;
        [sender addTarget:self action:@selector(confirmClick:) forControlEvents:UIControlEventTouchUpInside];
        _confirmBtn = sender;
    }
    return _confirmBtn;
}
- (void)confirmClick:(UIButton *)sender{
    if (_type == YES) {
        if (_block) {
            _block([NSString stringWithFormat:@"%ld-%ld-%ld",(long)yearString,(long)monthString,(long)dayString]);
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }else{
       [self.delegate getCalenderDate:[NSString stringWithFormat:@"%ld-%ld-%ld",(long)yearString,(long)monthString,(long)dayString]];
    }
}
@end
